<?php
namespace app\common\helper;

class DateHelper
{
    /**
     * 格式化时间戳
     * @param int $timestamp 时间戳
     * @param string $format 格式
     * @return string
     */
    public static function format($timestamp, $format = 'Y-m-d H:i:s')
    {
        return date($format, $timestamp);
    }

    /**
     * 获取友好的时间显示
     * @param int $timestamp 时间戳
     * @return string
     */
    public static function friendly($timestamp)
    {
        $diff = time() - $timestamp;
        
        if ($diff < 60) {
            return $diff . '秒前';
        } elseif ($diff < 3600) {
            return floor($diff / 60) . '分钟前';
        } elseif ($diff < 86400) {
            return floor($diff / 3600) . '小时前';
        } elseif ($diff < 2592000) {
            return floor($diff / 86400) . '天前';
        } else {
            return date('Y-m-d', $timestamp);
        }
    }

    /**
     * 获取指定日期范围
     * @param string $type 类型：today/yesterday/week/month/year
     * @return array
     */
    public static function getDateRange($type)
    {
        switch($type) {
            case 'today':
                $start = strtotime('today');
                $end = strtotime('tomorrow') - 1;
                break;
            case 'yesterday':
                $start = strtotime('yesterday');
                $end = strtotime('today') - 1;
                break;
            case 'week':
                $start = strtotime('monday this week');
                $end = strtotime('sunday this week') + 86399;
                break;
            case 'month':
                $start = strtotime('first day of this month');
                $end = strtotime('last day of this month') + 86399;
                break;
            case 'year':
                $start = strtotime('first day of january this year');
                $end = strtotime('last day of december this year') + 86399;
                break;
            default:
                return [];
        }
        
        return [$start, $end];
    }
} 